package com.cqupt.scanning.system.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqupt.scanning.handler.exception.MyException;
import com.cqupt.scanning.system.acl.entity.AclUser;
import com.cqupt.scanning.system.acl.service.AclUserService;
import com.cqupt.scanning.system.admin.entity.Department;
import com.cqupt.scanning.system.admin.entity.DeviceFoot;
import com.cqupt.scanning.system.admin.entity.DeviceThree;
import com.cqupt.scanning.system.admin.entity.Organization;
import com.cqupt.scanning.system.admin.entity.vo.DeviceVO;
import com.cqupt.scanning.system.admin.mapper.DeviceThreeMapper;
import com.cqupt.scanning.system.admin.service.DepartmentService;
import com.cqupt.scanning.system.admin.service.DeviceThreeService;
import com.cqupt.scanning.system.admin.service.OrganizationService;
import com.cqupt.scanning.system.utils.common.PasswordEncryption;
import com.cqupt.scanning.system.utils.result.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 宋宝梁
 * @since 2021-07-06
 */
@Service
public class DeviceThreeServiceImpl extends ServiceImpl<DeviceThreeMapper, DeviceThree> implements DeviceThreeService {

    private AclUserService aclUserService;
    private OrganizationService organizationService;
    private DepartmentService departmentService;

    @Autowired
    public DeviceThreeServiceImpl(AclUserService aclUserService,
                                 OrganizationService organizationService,
                                 DepartmentService departmentService) {
        this.aclUserService = aclUserService;
        this.organizationService = organizationService;
        this.departmentService = departmentService;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public R insert(DeviceVO deviceVO) {
        try {
            int count = aclUserService.count(new QueryWrapper<AclUser>().eq("username", deviceVO.getUsername().trim()));
            if (count >= 1) return R.error().message("用户名已存在");
            DeviceThree deviceThree = new DeviceThree();
            AclUser aclUser = new AclUser();
            aclUser.setUsername(deviceVO.getUsername().trim());
            aclUser.setPassword(PasswordEncryption.execute(deviceVO.getPassword().trim()));
            aclUser.setNote("0");
            aclUser.setType(deviceVO.getDevName());
            BeanUtils.copyProperties(deviceVO, deviceThree);
            aclUserService.save(aclUser);
            deviceThree.setUserId(aclUser.getId());
            baseMapper.insert(deviceThree);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(20001, "添加失败，请重新操作");
        }
    }

    @Override
    public R update(DeviceVO deviceVO) {
        DeviceThree deviceThree = new DeviceThree();
        BeanUtils.copyProperties(deviceVO, deviceThree);
        int id = baseMapper.updateById(deviceThree);
        if (id <= 0) return R.error().message("Update failed");
        return R.ok();
    }

    @Override
    public R delById(Integer id) {
        try {
            DeviceThree deviceThree = baseMapper.selectById(id);
            aclUserService.removeUserById(deviceThree.getUserId());
            baseMapper.deleteById(id);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(20001, "删除失败，请重新操作");
        }
    }

    @Override
    public R getAll(Page<DeviceThree> page, String query) {
        IPage<DeviceThree> devPage = baseMapper.selectPage(page, new QueryWrapper<DeviceThree>().like("dev_num", query));
        List<DeviceThree> records = devPage.getRecords();
        for (DeviceThree record : records) {
            Department department = departmentService.getOne(new QueryWrapper<Department>()
                    .select("dep_name", "org_num").eq("id", record.getDepId()));
            Organization organization = organizationService.getOne(new QueryWrapper<Organization>()
                    .select("org_name", "org_address").eq("org_num", department.getOrgNum()));
            record.setDepName(department.getDepName())
                    .setOrgName(organization.getOrgName())
                    .setOrgAdress(organization.getOrgAddress());
        }
        return R.ok().data("devices", devPage);
    }
}
